package com.usermanage.ServiceImpl;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.usermanage.Mapping.CompanyMapping;
import com.usermanage.Mapping.GroupMapping;
import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.SecurityConfig.UserDetail;
import com.usermanage.Service.CompanyService;
import com.usermanage.Service.GroupService;
import com.usermanage.beanParam.CompanyBean;
import com.usermanage.beanParam.ContractBean;
import com.usermanage.beanParam.GroupBean;
import com.usermanage.beanParam.UserBean;
import com.usermanage.dataDao.JpaDao;
import com.usermanage.util.LevelUtil;
import com.usermanage.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * Created by 過客 on 2018/3/9
 */
@Service("companyServiceImpl")
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyMapping companyMapping;
    @Resource
    private JpaDao jpaDao;


    @Override
    public List<CompanyBean> findAllForSelect() {
//        return companyMapping.getListByPage("SELECT id,company_name from u_company ");
        return companyMapping.findAll();
    }

    @Override
    public List<CompanyBean> findAllList(String key, String page, String size) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT * FROM u_company where 1=1 ");
        if (StringUtils.isNotBlank(key)){
            buffer.append(" and company_name like '%").append(key).append("%'");
        }
        if(Integer.valueOf(size)>0){
            buffer.append(" and company_state != '00' order by id desc limit ").append((Integer.valueOf(page)-1) * Integer.valueOf(size)).append(",").append(size);
        }
        List<CompanyBean> list = jpaDao.getListByPage(buffer.toString(),CompanyBean.class);
        for (CompanyBean bean:list) {
            if ("11".equals(bean.getCompany_state())){
                bean.setCompany_state("登记");
            }else if ("22".equals(bean.getCompany_state())){
                bean.setCompany_state("启用");
            }else{
                bean.setCompany_state("禁用");
            }
        }
        return list;
    }

    @Override
    public int getAllUserListCount(String key) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT count(1) FROM u_company where 1=1 ");
        if (StringUtils.isNotBlank(key)){
            buffer.append(" and company_name like '%").append(key).append("%' and company_state != '00'");
        }
        return jpaDao.getListCountByPage(buffer.toString());
    }

    @Override
    public Boolean isExisit(CompanyBean bean) {
        Example<CompanyBean> example = Example.of(bean);
        return companyMapping.exists(example);
    }

    @Override
    public CompanyBean save(CompanyBean bean) {
        return companyMapping.save(bean);
    }

    @Override
    public Optional<CompanyBean> findById(int id) {
        return companyMapping.findById(id);
    }

    @Override
    public List<CompanyBean> findAllListByCompanyId(String company_id) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT * FROM u_company where 1=1 and id ='").append(company_id).append("'");
        List<CompanyBean> list = jpaDao.getListByPage(buffer.toString(),CompanyBean.class);
        return list;
    }
}
